const Router = require('@koa/router')
const router = new Router()

const { join } = require('path')

const multer = require('@koa/multer')
const { storageMD5, cbp } = require('../tool/storage')

const uploadImage = multer({
  storage: new storageMD5({
    dest: join(__dirname, '../../public/image/'),
  }),
  fileFilter: cbp(async (req, file) => {
    if (!['image/png', 'image/jpeg'].includes(file.mimetype)) return false
    return true
  }),
  limits: {
    fileSize: 1024 ** 2 * 5,
    parts: 1024 ** 2 * 60,
  },
})

router.post('/image', uploadImage.array('file', 16), (ctx) => {
  console.log()
  ctx.body = {
    code: 200,
    baseUrl: '/image/',
    files: ctx.files.map(({ name, originalname }) => ({ name, originalname })),
  }
})

module.exports = router.routes()
